package xcmg.device.web.agent;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.dao.entity.agent.ServiceBorrowOutDetailDO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agent.ServiceBorrowOutDetailService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.agent.ServiceBorrowOutDetailVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;

/**
 * ServiceBorrowOutDetail Controller 实现类
 *
 * @author huhuawei
 */
@RestController
@RequestMapping("/serviceBorrowOutDetail")
public class ServiceBorrowOutDetailController extends BasicController {
    private static final Logger logger = LoggerFactory.getLogger(ServiceBorrowOutDetailController.class);
    @Autowired
    private ServiceBorrowOutDetailService serviceBorrowOutDetailService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "服务借出单明细分页列表信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<ServiceBorrowOutDetailVO>> list(@RequestBody SearchCommonVO<ServiceBorrowOutDetailVO> condition) {
        PageCommonVO<ServiceBorrowOutDetailVO> pageCommonVO = serviceBorrowOutDetailService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "服务借出单明细-保存")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> save(@RequestBody ServiceBorrowOutDetailVO serviceBorrowOutDetailVO) {
        if (null == serviceBorrowOutDetailVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode code = serviceBorrowOutDetailService.save(serviceBorrowOutDetailVO, getUserId(), getUserName(), getOrgId());
            return actionResult(code);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询服务借出单明细详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult<ServiceBorrowOutDetailVO> item(String id) {
        ServiceBorrowOutDetailDO serviceBorrowOutDetailDO = serviceBorrowOutDetailService.queryById(id);
        ServiceBorrowOutDetailVO serviceBorrowOutDetailVO = new ServiceBorrowOutDetailVO();
        BeanUtils.copyProperties(serviceBorrowOutDetailDO, serviceBorrowOutDetailVO);
        return actionValueResult(serviceBorrowOutDetailVO);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除服务借出单明细数据")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> delete(@RequestBody ServiceBorrowOutDetailVO serviceBorrowOutDetailVO) {
        ErrorCode errorCode = null;
        try {
            errorCode = serviceBorrowOutDetailService.batchDelete(serviceBorrowOutDetailVO.getListId());
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/download",method = RequestMethod.POST)
    @ApiOperation("下载零件批量导入模板")
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        serviceBorrowOutDetailService.download();
    }


    @ResponseBody
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiOperation("零件批量导入")
    @FastMappingInfo(needLogin = true)
    public ActionResult importExcel(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result=new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            InputStream in = file.getInputStream();
            result= serviceBorrowOutDetailService.importExcel(in, request.getParameter("x-warehouseCode"), request.getParameter("x-areaCode"), getOrgId());
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ApiOperation(value = "删除借用单明细数据")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> batchDelete(@RequestBody List<String> ids) {
        if (null == ids || ids.isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode;
        try {
            errorCode = serviceBorrowOutDetailService.delete(ids,getOrgId());
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }
}
